package com.nerve.android.cloudoffice.app.activity.department;

import org.nerve.android.NerveActivityGroup;
import org.nerve.android.annotation.Acvitity;
import org.nerve.android.annotation.ViewOnId;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;

import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.app.group.IndexGroupActivity;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.domain.Department;
import com.nerve.android.cloudoffice.service.data.DepartmentData;

/**
 * 
 * @project Cloudoffice AddDepartmentActivity.java
 * @datetime Aug 12, 2013 4:21:13 PM
 * @description 
 *    
 * @author 集成显卡 IBM 
 *			zxingming@qq.com
 *			zengxm@cn.ibm.com
 *
 * @change log:
 *
 */
@SuppressLint("HandlerLeak")
@Acvitity(layout=R.layout.activity_department_add)
public class AddDepartmentActivity extends NavigationActivity{

	@ViewOnId(id=R.id.name)
	private EditText nameET;
	
	private Handler handler = new Handler(){
		public void handleMessage(Message msg){
			hideProgressDialog();
			switch(msg.what){
			case WORKER_OK:
				toast("保存成功");
				onNavigateBackButtonClick();
				break;
			case WORKER_FAIL:
				toast(Const.ERROR);
				break;
			}
		}
	};
	
	@Override
	protected void initUI() {
		super.initUI();
		
		setNavTitle("新增部门");
		customNavigateRightButton("保存", true);
	}
	
	@Override
	protected void onNavigateRightButtonClick() {
		super.onNavigateRightButtonClick();
		
		String name = nameET.getText().toString();
		if(name.trim().length() == 0){
			toast("部门名称不能为空");
			return ;
		}
		
		try{
			save(name);
		}catch(Exception e){
			e.printStackTrace(System.out);
		}
	}
	
	private void save(String name) throws Exception{
		showProgressDialog(null, "正在保存部门，请稍候...");
		
		new DepartmentWorker().execute(name);
	}
	
	@Override
	protected void onNavigateBackButtonClick() {
		NerveActivityGroup group = getActivityGroup(IndexGroupActivity.class);
		group.back();
	}
	
	/**
	 * @project: Cloudoffice
	 * @file: AddDepartmentActivity.java
	 * @package: com.nerve.android.cloudoffice.app.activity.department
	 * @description:
	 *
	 * @author: 集成显卡	1053214511@qq.com
					IBM GDC, zengxm@cn.ibm.com
	 * @date&time: Aug 15, 2013
	 * @change log:
	 */
	private class DepartmentWorker extends AsyncTask<String, Void, Integer>{

		@Override
		protected Integer doInBackground(String... params) {
			if(params.length < 1)
				return null;
		
			Department d = new Department();
			d.setId((int)System.currentTimeMillis()/1000/60);
			d.setName(params[0]);
			
			Reply re = DepartmentData.addDepartment(d);
			if(re.isError())
				Const.ERROR = re.getMessage().toString();
			
			return re.isError()? WORKER_FAIL: WORKER_OK;
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			handler.sendEmptyMessage(result);
		}

	}
}